package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IPrivateMoveMsg;
import com.fingard.dsp.bank.reqrespobj.privatemove.ReqPrivateMoveMsgQry;
import com.fingard.dsp.bank.reqrespobj.privatemove.RetAtsPrivateMoveDetail;
import com.fingard.dsp.bank.reqrespobj.privatemove.RetAtsPrivateMoveMsg;
import com.fingard.io.FileHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01PrivateMoveMsg extends CMB01Base implements IPrivateMoveMsg {
    @Override
    public RetAtsPrivateMoveMsg getPrivateMoveMsg(ReqPrivateMoveMsgQry qry) throws Exception {
        RetAtsPrivateMoveMsg retAts = new RetAtsPrivateMoveMsg(reqHeader);
        retAts.writeStart();
        String tmpRespCode ="";
        String tmpRespInfo ="";
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDAFYCQY";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            输入	TDAFYCQYX	输入属性
            xmlWriter.writeStartElement("TDAFYCQYX");
//            直联企业系统客户号	COREID	C (10)	否
            xmlWriter.writeElementString("COREID",bankFront.org );
//            对接企业类型	COTYPE	C (1)	否	1核心企业 2服务商
            xmlWriter.writeElementString("COTYPE", "1");
//            核心企业组织机构代码	SCOCOD	C (32)	可
            xmlWriter.writeElementString("SCOCOD", "");
//            经销商代码	DLENUM	C (40)	可
            xmlWriter.writeElementString("DLENUM", qry.DealerCode);
//            经销商组织机构代码	DLECOD	C (32)	可	经销商代码、经销商组织机构代码二者必输其一
            xmlWriter.writeElementString("DLECOD", "");
//            反馈日期起	SATDAT	D	可
            xmlWriter.writeElementString("SATDAT",qry.DiscoveryMoveDate );
//            反馈日期止	ENDDAT	D	可
            xmlWriter.writeElementString("ENDDAT",qry.DiscoveryMoveDate );
//            监管状态	SURSTS	C(30)	可	01在途02在库03私移04私售05正常移动06正常07质损08其它 多种状态用英文逗号隔开
            xmlWriter.writeElementString("SURSTS","03" );

            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
                        tmpRespCode ="0";
                        tmpRespInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        while (tmpBkReader.read("/CMBSDKPGK/TDAFYCQYY")){
                            RetAtsPrivateMoveDetail detail = new RetAtsPrivateMoveDetail();
//                            经销商名称	DLENAM	Z(100)	否	经销商名称
                            detail.DLR_CODE  = tmpBkReader.readedXElement.getSingleText("DLENAM");
//                            经销商代码	DLENUM	C (40)	可
                            detail.DLR_SHORT_NAME  = tmpBkReader.readedXElement.getSingleText("DLENUM");
//                            底盘号/车架号	CHANUM	C(40)	否
                            detail.VIN = tmpBkReader.readedXElement.getSingleText("CHANUM");
//                            车型	CARTYP	Z(40)	可
                            detail.CAR_TYPE_CODE  = tmpBkReader.readedXElement.getSingleText("CARTYP");
//                            监管状态	SURSTS	C(2)	可	01在途02在库03私移04私售05正常移动06正常07质损08其它
                            detail.DISCOVERY_MOVE_DATE = tmpBkReader.readedXElement.getSingleText("SURSTS");
//                            反馈日期	FEDDAT	D	可
                            detail.DISCOVERY_MOVE_DATE = tmpBkReader.readedXElement.getSingleText("FEDDAT");
                            retAts.writeRetDetail(detail);
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            tmpRespCode ="-1";
            tmpRespInfo = ex.getMessage();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
